/*
 * Copyright (C), 2002-2017, 苏宁易购电子商务有限公司
 * FileName: ActivityRealtimeDataServiceImpl.java
 * Author:   15060460
 * Date:     2017年3月27日 下午5:49:40
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.sawp.member.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suning.sawp.intf.store.StoreInfoService;
import com.suning.sawp.member.dao.ActivityRealtimeDataDao;
import com.suning.sawp.member.service.ActivityRealtimeDataService;
import com.suning.sawp.member.vo.ActivityRealtimeDataVo;
import com.suning.sawp.member.vo.GridViewVo;
import com.suning.sawp.member.vo.QueryRealTimeDataVo;

/**
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author 15060460
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class ActivityRealtimeDataServiceImpl implements ActivityRealtimeDataService {

	@Resource
	private ActivityRealtimeDataDao activityRealtimeDataDao;

	@Autowired
	StoreInfoService storeInfoService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.suning.sawp.member.service.ActivityRealtimeDataService#
	 * queryAreaRealtimeData(com.suning.sawp.member.dto. QueryRealTimeDataVo)
	 */
	@Override
	public ActivityRealtimeDataVo queryRealtimeDataHq(QueryRealTimeDataVo queryVo) {
		ActivityRealtimeDataVo vo = activityRealtimeDataDao.queryRealtimeDataHq(queryVo);
		if (vo != null) {
			vo.setDynamicName(queryVo.getAreaName());
		}
		return vo;
	}

	@Override
	public GridViewVo<ActivityRealtimeDataVo> queryRealtimeDataStore(QueryRealTimeDataVo queryVo) {
		GridViewVo<ActivityRealtimeDataVo> grid = new GridViewVo<ActivityRealtimeDataVo>();
		// 门店数量有限，使用假分页
		List<ActivityRealtimeDataVo> datas = activityRealtimeDataDao.queryRealtimeDataStore(queryVo, null, null);

		grid.setCount(Long.valueOf(datas.size()));
		grid.setPageNo(queryVo.getPageNo());
		grid.setPageSize(queryVo.getPageSize());
		// 分页
		int start = queryVo.getStart().intValue();
		int limit = start + queryVo.getLimit().intValue();
		limit = limit > datas.size() ? datas.size() : limit;

		// 导出的时候，查询全部
		if (queryVo.getPageNo().longValue() == -1L) {
			ActivityRealtimeDataVo totalData = computeTotalData(datas);
			grid.setData(mergeTotalData(totalData, datas));
			return grid;
		}

		List<ActivityRealtimeDataVo> subDatas = datas.subList(start, limit);
		// 只有首页才给总计
		if (start == 0) {
			ActivityRealtimeDataVo totalData = computeTotalData(datas);
			grid.setData(mergeTotalData(totalData, subDatas));
		} else {
			grid.setData(subDatas);
		}
		return grid;
	}

	@Override
	public List<ActivityRealtimeDataVo> queryRealtimeDataCate(QueryRealTimeDataVo queryVo) {
		List<ActivityRealtimeDataVo> datas = activityRealtimeDataDao.queryRealtimeDataCate(queryVo);
		if (!datas.isEmpty()) {
			ActivityRealtimeDataVo totalData = computeTotalData(datas);
			return mergeTotalData(totalData, datas);
		}
		return datas;
	}

	@Override
	public GridViewVo<ActivityRealtimeDataVo> queryRealtimeDataEmployee(QueryRealTimeDataVo queryVo) {
		GridViewVo<ActivityRealtimeDataVo> grid = new GridViewVo<ActivityRealtimeDataVo>();
		List<ActivityRealtimeDataVo> datas = null;
		if (StringUtils.isNotEmpty(queryVo.getEmployeeId())) {// 督导
			datas = activityRealtimeDataDao.queryRealtimeDataEmployee(queryVo);
		} else if (StringUtils.isNotEmpty(queryVo.getCateCode())) {// 非督导，有品类编码
			datas = activityRealtimeDataDao.queryRealtimeDataEmployeeByCateCode(queryVo);
		}
		grid.setCount(CollectionUtils.isEmpty(datas) ? Long.valueOf(0) : Long.valueOf(datas.size()));
		if (CollectionUtils.isNotEmpty(datas)) {

			// 导出的时候，传-1
			if (queryVo.getPageNo().longValue() == -1L) {
				// 计算总计
				ActivityRealtimeDataVo totalData = computeTotalData(datas);
				grid.setData(mergeTotalData(totalData, datas));
				return grid;
			}
			// 分页
			int start = queryVo.getStart().intValue();
			int limit = start + queryVo.getLimit().intValue();
			limit = limit > datas.size() ? datas.size() : limit;

			List<ActivityRealtimeDataVo> subDatas = datas.subList(start, limit);
			// 只有首页才给总计
			if (start == 0) {
				ActivityRealtimeDataVo totalData = computeTotalData(datas);
				grid.setData(mergeTotalData(totalData, subDatas));
			} else {
				grid.setData(subDatas);
			}

		}
		grid.setPageNo(queryVo.getPageNo());
		grid.setPageSize(queryVo.getPageSize());
		return grid;
	}

	@Override
	public GridViewVo<ActivityRealtimeDataVo> queryRealtimeDataPlace(QueryRealTimeDataVo queryVo) {
		GridViewVo<ActivityRealtimeDataVo> grid = new GridViewVo<ActivityRealtimeDataVo>();
		// 用假分页
		List<ActivityRealtimeDataVo> datas = activityRealtimeDataDao.queryRealtimeDataPlace(queryVo);
		grid.setCount(Long.valueOf(datas.size()));
		if (CollectionUtils.isNotEmpty(datas)) {

			if (queryVo.getPageNo().longValue() == -1L) {
				// 计算总计
				ActivityRealtimeDataVo totalData = computeTotalData(datas);
				grid.setData(mergeTotalData(totalData, datas));
				return grid;
			}

			// 分页
			int start = queryVo.getStart().intValue();
			int limit = start + queryVo.getLimit().intValue();
			limit = limit > datas.size() ? datas.size() : limit;

			List<ActivityRealtimeDataVo> subDatas = datas.subList(start, limit);
			// 只有首页才给总计
			if (start == 0) {
				ActivityRealtimeDataVo totalData = computeTotalData(datas);
				grid.setData(mergeTotalData(totalData, subDatas));
			} else {
				grid.setData(subDatas);
			}
		}
		grid.setPageNo(queryVo.getPageNo());
		grid.setPageSize(queryVo.getPageSize());
		return grid;
	}

	private ActivityRealtimeDataVo computeTotalData(List<ActivityRealtimeDataVo> datas) {
		if (datas.isEmpty()) {
			return null;
		}
		Integer totalNum = 0;
		BigDecimal expectTotalAmount = BigDecimal.ZERO;
		Integer expectLockTotalNum = 0;
		Integer expectLockSingleNum = 0;
		Integer expectLockSuitNum = 0;

		BigDecimal expectLockTotalAmount = BigDecimal.ZERO;
		BigDecimal expectLockSingleAmount = BigDecimal.ZERO;
		BigDecimal expectLockSuitAmount = BigDecimal.ZERO;

		Integer intentMemTotalNum = 0;
		Integer intentMemSingleNum = 0;
		Integer intentMemSuitNum = 0;

		BigDecimal intentMemTotalAmount = BigDecimal.ZERO;
		BigDecimal intentMemSingleAmount = BigDecimal.ZERO;
		BigDecimal intentMemSuitAmount = BigDecimal.ZERO;

		for (ActivityRealtimeDataVo vo : datas) {
			totalNum = totalNum + vo.getTotalNum();
			expectTotalAmount = expectTotalAmount.add(vo.getExpectTotalAmount());
			expectLockTotalNum = expectLockTotalNum + vo.getExpectLockTotalNum();
			expectLockSingleNum = expectLockSingleNum + vo.getExpectLockSingleNum();
			expectLockSuitNum = expectLockSuitNum + vo.getExpectLockSuitNum();

			expectLockTotalAmount = expectLockTotalAmount.add(vo.getExpectLockTotalAmount());
			expectLockSingleAmount = expectLockSingleAmount.add(vo.getExpectLockSingleAmount());
			expectLockSuitAmount = expectLockSuitAmount.add(vo.getExpectLockSuitAmount());

			intentMemTotalNum = intentMemTotalNum + vo.getIntentMemTotalNum();
			intentMemSingleNum = intentMemSingleNum + vo.getIntentMemSingleNum();
			intentMemSuitNum = intentMemSuitNum + vo.getIntentMemSuitNum();

			intentMemTotalAmount = intentMemTotalAmount.add(vo.getIntentMemTotalAmount());
			intentMemSingleAmount = intentMemSingleAmount.add(vo.getIntentMemSingleAmount());
			intentMemSuitAmount = intentMemSuitAmount.add(vo.getIntentMemSuitAmount());
		}
		ActivityRealtimeDataVo totalDataVo = new ActivityRealtimeDataVo();
		totalDataVo.setDynamicName("总计");
		totalDataVo.setTotalNum(totalNum);
		totalDataVo.setExpectTotalAmount(expectTotalAmount);
		totalDataVo.setExpectLockTotalNum(expectLockTotalNum);
		totalDataVo.setExpectLockSingleNum(expectLockSingleNum);
		totalDataVo.setExpectLockSuitNum(expectLockSuitNum);

		totalDataVo.setExpectLockTotalAmount(expectLockTotalAmount);
		totalDataVo.setExpectLockSingleAmount(expectLockSingleAmount);
		totalDataVo.setExpectLockSuitAmount(expectLockSuitAmount);

		totalDataVo.setIntentMemTotalNum(intentMemTotalNum);
		totalDataVo.setIntentMemSingleNum(intentMemSingleNum);
		totalDataVo.setIntentMemSuitNum(intentMemSuitNum);

		totalDataVo.setIntentMemTotalAmount(intentMemTotalAmount);
		totalDataVo.setIntentMemSingleAmount(intentMemSingleAmount);
		totalDataVo.setIntentMemSuitAmount(intentMemSuitAmount);

		return totalDataVo;
	}

	private List<ActivityRealtimeDataVo> mergeTotalData(ActivityRealtimeDataVo totalData, List<ActivityRealtimeDataVo> datas) {
		List<ActivityRealtimeDataVo> returnData = new ArrayList<ActivityRealtimeDataVo>();
		if (totalData != null) {
			returnData.add(totalData);
		}
		returnData.addAll(datas);
		return returnData;
	}

}
